package com.exam.pojo.page;

import com.exam.utils.CollUtils;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.List;

@Data
@AllArgsConstructor
@NoArgsConstructor
@ApiModel(description = "分页结果")
public class PageResult<T> {
    @ApiModelProperty("总条数")
    protected Long total = 0L;
    @ApiModelProperty("总页码数")
    protected Long pages = 0L;
    @ApiModelProperty("当前页数据")
    protected List<T> list = CollUtils.emptyList();

    public static <T> PageResult<T> of(Integer size, Long total, List<T> list) {
        if(CollUtils.isEmpty(list)){
            return empty();
        }
        double pages = Math.ceil(total.doubleValue() / size.doubleValue());
        return new PageResult<>(total,(long) pages,list);
    }

    public static <T> PageResult<T> empty(){
        return new PageResult<>();
    }
}
